# -*- coding: utf-8 -*-
"""
# @Time    : 2021/1/7 上午9:22
# @Author  : tgy
# url      : 
"""


class Solution:
    ...

    def threeSumClosest(self, nums, target) -> int:
        ...
        # 排序
        result = None
        nums.sort()
        nums_len = len(nums)
        for i in range(nums_len - 2):
            ...
            left, right = i + 1, nums_len - 1
            while left < right:
                ...
                three_sum = nums[i] + nums[left] + nums[right]
                print(i, left, right, three_sum)
                if three_sum < target:
                    left += 1
                elif three_sum > target:
                    right -= 1
                elif three_sum == target:
                    return target
                cur = abs(three_sum - target)

                if (result is None) or cur < abs(result - target):
                    result = three_sum

        return result


if __name__ == '__main__':
    ...
    solution = Solution()
    result = solution.threeSumClosest([0, 2, 1, -3], 1)
    print(result)
